package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Coupon;
import com.chixing.entity.Mycoupon;
import com.chixing.mapper.MycouponMapper;
import com.chixing.service.IMycouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */
@Service
public class MycouponServiceImpl implements IMycouponService {

    @Autowired
    private MycouponMapper mycouponMapper;
    @Override
    public Result getAll(Integer status) {
        QueryWrapper<Mycoupon> wrapper = new QueryWrapper<>();
        wrapper.gt("coupon_end_time", LocalDateTime.now()).eq("status",status); // 只查询未过期优惠券
        List<Mycoupon> couponList = mycouponMapper.selectList(wrapper);
        if (couponList != null && !couponList.isEmpty()) {
            return Result.getSuccess(couponList);
        }
        return Result.getFail("暂无可用优惠券");
    }

    @Override
    public Result save(Mycoupon mycoupon) {
        int rows = mycouponMapper.insert(mycoupon);
        if(rows >0)
            return Result.saveSuccess(mycoupon);
        return Result.saveFail(mycoupon);
    }
}
